{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check If String Is a Prefix of Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isPrefixString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查字符串是否为数组前缀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code> 和一个字符串数组 <code>words</code> ，请你判断 <code>s</code> 是否为 <code>words</code> 的 <strong>前缀字符串</strong> 。</p>\n",
    "\n",
    "<p>字符串 <code>s</code> 要成为 <code>words</code> 的 <strong>前缀字符串</strong> ，需要满足：<code>s</code> 可以由 <code>words</code> 中的前 <code>k</code>（<code>k</code> 为 <strong>正数</strong> ）个字符串按顺序相连得到，且 <code>k</code> 不超过 <code>words.length</code> 。</p>\n",
    "\n",
    "<p>如果 <code>s</code> 是 <code>words</code> 的 <strong>前缀字符串</strong> ，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"iloveleetcode\", words = [\"i\",\"love\",\"leetcode\",\"apples\"]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "s 可以由 \"i\"、\"love\" 和 \"leetcode\" 相连得到。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"iloveleetcode\", words = [\"apples\",\"i\",\"love\",\"leetcode\"]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>\n",
    "数组的前缀相连无法得到 s 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length &lt;= 20</code></li>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 1000</code></li>\n",
    "\t<li><code>words[i]</code> 和 <code>s</code> 仅由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-string-is-a-prefix-of-array](https://leetcode.cn/problems/check-if-string-is-a-prefix-of-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-string-is-a-prefix-of-array](https://leetcode.cn/problems/check-if-string-is-a-prefix-of-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"iloveleetcode\"\\n[\"i\",\"love\",\"leetcode\",\"apples\"]', '\"iloveleetcode\"\\n[\"apples\",\"i\",\"love\",\"leetcode\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n = len(s)\n",
    "        left = 0\n",
    "        for word in words:\n",
    "            if left == n: break\n",
    "            n2 = len(word)\n",
    "            if left+n2 <= n and s[left:left+n2] == word: left += n2\n",
    "            else: break\n",
    "        if left == n: return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        temp = ''\n",
    "        for word in words:\n",
    "            temp += word\n",
    "            if len(temp) > len(s):\n",
    "                return False\n",
    "            if temp == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n = len(s)\n",
    "        res = ''\n",
    "        for i in words:\n",
    "            res += i\n",
    "            if len(res) >= n:\n",
    "                break\n",
    "        return res == s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\r\n",
    "        for i in range(len(words)+1):\r\n",
    "            if ''.join(words[:i]) == s:\r\n",
    "                return True\r\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        slen = len(s)\n",
    "        words_str = \"\".join(words)\n",
    "        if not s or slen > len(words_str):\n",
    "            return False\n",
    "        if slen == len(words_str):\n",
    "            if s == words_str:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        start = 0\n",
    "        for idx, word in enumerate(words):\n",
    "            end = start + len(word)\n",
    "            if end < slen:\n",
    "                if word == s[start:end]:\n",
    "                    start = end\n",
    "                else:\n",
    "                    return False\n",
    "            elif end > slen:\n",
    "                return False\n",
    "            elif end == slen:\n",
    "                if word == s[start:end]:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        return any(''.join(words[:i+1]) == s for i in range(len(words)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        lis = []\n",
    "        for i in range(len(words)):\n",
    "            cot = 0\n",
    "            lis.append(words[i])\n",
    "            if ''.join(lis) == s:\n",
    "                cot = 1;\n",
    "                break\n",
    "        return (True if cot == 1 else False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(words) > 1 and len(words[0]) <= len(s):\n",
    "            if s == words[0]:\n",
    "                return True\n",
    "            else:\n",
    "                one = words.pop(1)\n",
    "                words[0] += one\n",
    "        return s == words[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(words) > 1 and len(words[0]) <= len(s):\n",
    "            if s == words[0]:\n",
    "                return True\n",
    "            else:\n",
    "                one = words.pop(1)\n",
    "                words[0] += one\n",
    "        return s == words[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        ans = \"\"\n",
    "        for word in words:\n",
    "            ans += word\n",
    "            if s == ans:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        sentence=''\n",
    "        for i in range (len(words)):\n",
    "            sentence += words[i]\n",
    "            if sentence == s :\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n = len(words)\n",
    "        l = 0\n",
    "        l2 = 0\n",
    "        m = len(s)\n",
    "        while l < m and l2 < n:\n",
    "            c = len(words[l2])\n",
    "            if l + c - 1 > m or s[l:l + c] != words[l2]:\n",
    "                return False\n",
    "            l += c\n",
    "            l2 += 1\n",
    "        if l < m:\n",
    "            return False\n",
    "        return True\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        t = ''\n",
    "        l = len(s)\n",
    "        for w in words:\n",
    "            t += w\n",
    "            if t != s[:len(t)]:\n",
    "                return False\n",
    "            if len(t) == l:\n",
    "                break\n",
    "        return len(t) == l "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        ss=''\n",
    "        for i in words:\n",
    "            ss+=i\n",
    "            if ss==s:\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        p = \"\"\n",
    "        for c in words:\n",
    "            p += c\n",
    "            if(s == p):\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        if not s or not words or len(s) > len(\"\".join(words)):\n",
    "            return False\n",
    "\n",
    "        result = False\n",
    "        word = \"\"\n",
    "        for i in words:\n",
    "            word += i\n",
    "            if s == word:\n",
    "                result = True\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        lens=len(s)\n",
    "        count=0\n",
    "        for i in range(len(words)):\n",
    "            count+=len(words[i])\n",
    "            if count==lens and s==''.join(words[:i+1]):\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(words) > 1 and len(words[0]) <= len(s):\n",
    "            if s == words[0]:\n",
    "                return True\n",
    "            else:\n",
    "                words = [words[0] + words[1]] + words[-(len(words)-2):]\n",
    "        return s == words[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        words1=''\n",
    "        for i in words:\n",
    "            words1+=i\n",
    "            if len(words1)>=len(s):\n",
    "                break\n",
    "        return words1==s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        for word in words:\n",
    "            for ch in word:\n",
    "                if i < n and ch == s[i]:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    # s 提前遍历完成或当前字符不匹配\n",
    "                    return False\n",
    "            if i == n:\n",
    "                # 此时满足前缀定义\n",
    "                return True\n",
    "        # 遍历完成 words 时 s 仍未遍历完成\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        for word in words:\n",
    "            if not s.startswith(word):\n",
    "                return False\n",
    "            s = s[len(word):]\n",
    "            if not s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        prefix_s = ''\n",
    "        for word in words:\n",
    "            prefix_s += word    \n",
    "            if s == prefix_s:\n",
    "                return True\n",
    "            \n",
    "            if len(prefix_s) > len(s):\n",
    "                return False\n",
    "\n",
    "        return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        t=''\n",
    "        for i in words:\n",
    "            t=t+i\n",
    "            if s==t:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        return any(com == s for com in accumulate(words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def isPrefixString(self, s, words):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type words: List[str]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        l= 0\n",
    "        i = 0\n",
    "        for idx in range(1,len(s)+1):\n",
    "            if i<len(words) and s[l:idx] == words[i]:\n",
    "                print(s[l:idx])\n",
    "                l = idx\n",
    "                i+=1\n",
    "        return l == len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        x = \"\"\n",
    "        for i in range (len(words)):\n",
    "            x += words[i]\n",
    "            if len(x) > len(s):\n",
    "                return False\n",
    "            if len(x) == len(s):\n",
    "                return x == s\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        res = \"\"\n",
    "        for w in words:\n",
    "            res = res + w\n",
    "            if res ==s:\n",
    "                return True\n",
    "        return False        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        t=\"\"\n",
    "        for i in range(len(words)):\n",
    "           t+=words[i]\n",
    "           if t==s:\n",
    "               return True\n",
    "        return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        ans = \"\"\n",
    "        for word in words:\n",
    "            ans += word\n",
    "            if ans == s:\n",
    "                return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        ans = ''\n",
    "        for w in words:\n",
    "            ans = ans + w\n",
    "            if s == ans:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        l = len(s)\n",
    "        ss = ''\n",
    "        for w in words:\n",
    "            if len(ss) < l:\n",
    "                ss = ss+w\n",
    "            else:\n",
    "                break\n",
    "        \n",
    "        if s == ss:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        res=''\n",
    "        i=0\n",
    "        while len(res)<len(s) and i<len(words):\n",
    "            res+=words[i]\n",
    "            i+=1\n",
    "        return res==s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        prefix_word = \"\"\n",
    "        for word in words:\n",
    "            prefix_word += word\n",
    "            if prefix_word == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        ans = \"\"\n",
    "        for w in words:\n",
    "            ans += w\n",
    "            if ans == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n = len(s)\n",
    "        start = 0\n",
    "        \n",
    "        for word in words:\n",
    "            if start == n:\n",
    "                return True\n",
    "            if start + len(word) > n:\n",
    "                return False\n",
    "            if s[start: start + len(word)] != word:\n",
    "                return False\n",
    "            start = start + len(word)\n",
    "\n",
    "        return start == n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        for word in words:\n",
    "            for ch in word:\n",
    "                if i<n and ch==s[i]:\n",
    "                    i += 1\n",
    "                else:return False\n",
    "            if i==n:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(words) > 1 and len(words[0]) <= len(s):\n",
    "            if s == words[0]:\n",
    "                return True\n",
    "            else:\n",
    "                words = [words[0] + words[1]] + words[-(len(words)-2):]\n",
    "        return s == words[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(words) > 1 and len(words[0]) <= len(s):\n",
    "            if s == words[0]:\n",
    "                return True\n",
    "            else:\n",
    "                words = [words[0] + words[1]] + words[-(len(words)-2):]\n",
    "        return s == words[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        i=\"\"\n",
    "        for j in words:\n",
    "            i+=j\n",
    "            if i==s:\n",
    "                return True\n",
    "                break\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        res = ''\n",
    "        for i in range(len(words)):\n",
    "            res += words[i]\n",
    "            if res == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(words) > 1 and len(words[0]) <= len(s):\n",
    "            if s == words[0]:\n",
    "                return True\n",
    "            else:\n",
    "                words[0] += words.pop(1)\n",
    "        return s == words[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        i=0\n",
    "        n=len(s)\n",
    "        for word in words:\n",
    "            for ch in word:\n",
    "                if i<n and ch==s[i]:\n",
    "                    i+=1\n",
    "                else:\n",
    "                    return False\n",
    "            if i==n:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        i = 0;\n",
    "        count = len(s);\n",
    "        for w in words:\n",
    "            for ch in w:\n",
    "                if i < count and ch == s[i]:\n",
    "                    i += 1;\n",
    "                else:\n",
    "                    return False;\n",
    "            if i == count:\n",
    "                return True;\n",
    "        return False;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        totallen=len(s)\n",
    "        i=0\n",
    "        for word in words:\n",
    "            for j in range(len(word)):\n",
    "                if s[i]==word[j] :\n",
    "                    i+=1\n",
    "                    j+=1\n",
    "                    totallen-=1\n",
    "                    if totallen==0:\n",
    "                        if j!=len(word):\n",
    "                            return False\n",
    "                        else:\n",
    "                            return True\n",
    "                else:\n",
    "                    return False\n",
    "        return False    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        return any(''.join(words[:i+1]) == s  for i in range(len(words)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        if s == \"\".join(words):\n",
    "            return True\n",
    "        ws = \"\"\n",
    "        for word in words:\n",
    "            if s.startswith(ws):\n",
    "                if s == ws:\n",
    "                    return True\n",
    "                ws += word\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n=len(words)\n",
    "        for i in range(n):\n",
    "            newst=words[:i+1]\n",
    "            ls=''.join(newst)\n",
    "            if ls==s:\n",
    "            # if s.count(s)==1:    \n",
    "                return True\n",
    "        return False\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: list[str]) -> bool:\n",
    "        t = \"\"\n",
    "        for i in words:\n",
    "            t += i\n",
    "            if s == t:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        while len(s) > 0:\n",
    "            if len(''.join(words))<len(s):\n",
    "                return False\n",
    "            if len(s)<len(words[0]):\n",
    "                return False\n",
    "            if words[0] != s[:len(words[0])]:\n",
    "                return False\n",
    "\n",
    "            else:\n",
    "                s = s[len(words[0]):]\n",
    "                words.pop(0)\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        return any(\"\".join(words[:i + 1]) == s for i in range(len(words)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        ans = \"\"\n",
    "        for w in words:\n",
    "            ans += w\n",
    "            if ans[0] != s[0]:\n",
    "                return False\n",
    "            if ans == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        for word in words:\n",
    "            l = len(word)\n",
    "            if len(s) < l:\n",
    "                return False\n",
    "\n",
    "            if s[:l] == word:\n",
    "                pass\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "            s = s[l:]\n",
    "\n",
    "            if len(s) == 0:\n",
    "                return True\n",
    "\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "      temp = ''\n",
    "      for i in words:\n",
    "        temp += i\n",
    "        length =  len(temp)\n",
    "        cpmLength = len(s)\n",
    "        if length >= cpmLength:\n",
    "          print(temp)\n",
    "          return s == temp\n",
    "      return False\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        for i in words:\n",
    "            n=len(i)\n",
    "            if s!='' and i!=s[:n]:\n",
    "                return False\n",
    "            s=s[n:]\n",
    "        return s==''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        for word in words:\n",
    "            for ch in word:\n",
    "                if i < n and ch == s[i]:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    # s 提前遍历完成或当前字符不匹配\n",
    "                    return False\n",
    "            if i == n:\n",
    "                # 此时满足前缀定义\n",
    "                return True\n",
    "        # 遍历完成 words 时 s 仍未遍历完成\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        if len(''.join(words))<len(s):\n",
    "            return False        \n",
    "        while len(s) > 0:\n",
    "            if len(s)<len(words[0]):\n",
    "                return False\n",
    "            if words[0] != s[:len(words[0])]:\n",
    "                return False\n",
    "\n",
    "            else:\n",
    "                s = s[len(words[0]):]\n",
    "                words.pop(0)\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n=len(words)\n",
    "        for i in range(n):\n",
    "            newst=words[:i+1]\n",
    "            ls=''.join(newst)\n",
    "            if ls==s:\n",
    "        \n",
    "                return True\n",
    "        return False\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        res = \"\"\n",
    "        for word in words:\n",
    "            res += word\n",
    "            if res == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        x = ''.join(words)\n",
    "        return x.startswith(s) and len(s) in list(accumulate(len(w) for w in words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### 340/349--pass\n",
    "# class Solution:  \n",
    "#     def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "#         words=\"\".join(words)\n",
    "#         n=len(s)\n",
    "#         return len(words)>=n and s==words[:n] \n",
    "\n",
    "class Solution:  \n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        n=len(s)\n",
    "        tmp=\"\"\n",
    "        i=0\n",
    "        for w in words:\n",
    "            m=len(w)\n",
    "            tmp+=w \n",
    "            if i+m==n and tmp==s:\n",
    "                return True\n",
    "            if i+m>n or s[i:i+m]!=w:\n",
    "                return False\n",
    "            i+=m\n",
    "        return i==n\n",
    "        \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        return any(''.join(words[:i+1]) == s for i in range(len(words)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        res = ''\n",
    "        for w in words:\n",
    "            res += w\n",
    "            if res == s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        start=0\n",
    "        idx=0\n",
    "        n=len(s)\n",
    "        m=len(words)\n",
    "        while start<n and idx<m:\n",
    "            end=start+len(words[idx])\n",
    "            if end<=n:\n",
    "                if s[start:end]!=words[idx]:\n",
    "                    return False\n",
    "                    break\n",
    "                idx+=1\n",
    "                start=end\n",
    "            else:\n",
    "                return False\n",
    "        if start<n:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        x=''\n",
    "        x=x+words[0]\n",
    "        \n",
    "        i=1\n",
    "        while len(x)<=len(s) and i<len(words):\n",
    "            if x==s:return True\n",
    "            x=x+words[i]\n",
    "            i+=1\n",
    "        if x==s :return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPrefixString(self, s: str, words: List[str]) -> bool:\n",
    "        \n",
    "        for i in range(1,len(s)+1):\n",
    "            if s==''.join(words[:i]):\n",
    "                return True\n",
    "        return False\n",
    "        \n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
